import requests
from lxml import etree
if __name__ == "__main__":
    headers = {
        'user-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    #爬取页面源码数据
    url = 'https://sz.58.com/ershoufang/'
    page_text = requests.get(url=url,headers=headers).text

    #数据解析
    tree = etree.HTML(page_text)
    #存储的div标签对象
    div_list = tree.xpath('//section[@class="list"]/div')
    fp = open('58.txt','w',encoding='utf-8')
    for div in div_list:
        title = div.xpath('a//h3/text()')[0]
        print(title)
        fp.write(title+'\n')